﻿using ProjBackEnd.Areas.Financial.Models;
using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Financial.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class InvoiceOptController : ApiController
    {
        /// <summary>
        /// 获取发票信息
        /// eg.: 0000000001|TST|WEB|20|1||1|B||||||
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|每页记录数|当前页数（从1开始）|建档人（空值=所有）|发票pid（空值=所有）|发票类别（B-我方为购方；S-我方为销方；空值=所有）|是否核销（Y-已核销；N-未核销；空值=所有）|发票类型（对应CD_BASIC.ATTRCD = 'FPLX'；空值=所有）|购方税号（空值=所有）|购方全称（空值=所有）|购方id（0=所有）|销方id（0=所有）|发票id（0=所有）|开票开始时间|开票结束时间</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/invoiceopt")]
        public RespInvoice GetInvoice(string key)
        {
            RespInvoice resp = new RespInvoice();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 17)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strCreateid = strsKeyOriginal[5];
            string strPid = strsKeyOriginal[6];
            string strInvoCatelog = strsKeyOriginal[7];
            string strEndlb = strsKeyOriginal[8];
            string strInvotp = strsKeyOriginal[9];
            string strBuyerTaxcode = strsKeyOriginal[10];
            string strBuyer = strsKeyOriginal[11];
            string strGenSdt = strsKeyOriginal[15];
            string strGenEdt = strsKeyOriginal[16];

            int iPageSize, iCurrPage, iCusid_buyer, iCusid_seller, iInvoid;
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) || !int.TryParse(strsKeyOriginal[4], out iCurrPage) || !int.TryParse(strsKeyOriginal[12], out iCusid_buyer) || !int.TryParse(strsKeyOriginal[13], out iCusid_seller) || !int.TryParse(strsKeyOriginal[14], out iInvoid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetInvoiceInfo(strCreateid, strComcd, iPageSize, iCurrPage, strPid, strInvotp, strInvoCatelog, strEndlb, strBuyerTaxcode, strBuyer, iCusid_buyer, iCusid_seller, iInvoid, strGenSdt, strGenEdt);
            return resp;
        }

        /// <summary>
        /// 修改发票信息
        /// </summary>
        /// <param name="_reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/invoiceopt")]
        public RespFinaNullResultInfo UpdateInvoice(ReqInvoice _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.UpdateInvoiceInfo(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.invoinfo);
            return resp;
        }

        /// <summary>
        /// 发票加入结算单
        /// </summary>
        /// <param name="_reqInfo">发票结算请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/invoicesettle")]
        public RespFinaNullResultInfo InvoiceSettle(ReqFninvoRelation _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.InvoiceSettle(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.fnid, _reqInfo.optlb, _reqInfo.invofnlist);
            return resp;
        }

        /// <summary>
        /// 结算单加入发票
        /// </summary>
        /// <param name="_reqInfo">发票结算请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/fnrelateinvo")]
        public RespFinaNullResultInfo FnRelateInvo(ReqFninvoRelation _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }
            resp = financialLogic.FnRelateInvo(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.invoid, _reqInfo.optlb, _reqInfo.invofnlist);
            return resp;
        }

        /// <summary>
        /// 根据发票id获取相关的结算单列表
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|发票id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/fninvo")]
        public RespFNInvo GetFNInvo(string key)
        {
            RespFNInvo resp = new RespFNInvo();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            int iInvoid;
            if (!int.TryParse(strsKeyOriginal[3], out iInvoid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            return financialLogic.GetFNInvo(iInvoid);

        }
        /// <summary>
        /// 根据结算单id获取相关发票列表
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|结算单id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/invoicefn")]
        public RespInvoFN GetInvoFN(string key)
        {
            RespInvoFN resp = new RespInvoFN();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            int iFnid;
            if (!int.TryParse(strsKeyOriginal[3], out iFnid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            return financialLogic.GetInvoFN(iFnid);

        }
    }
}
